
package com.runshu.newsmartark.fragment;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.runshu.newsmartark.R;
import com.runshu.newsmartark.base.BaseLazyFragmentPlus;
import com.runshu.newsmartark.base.BoxBaseActivity;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.common.ConstantUrl;
import com.runshu.newsmartark.util.common.SharedPreferencesUtils;
import com.runshu.newsmartark.util.common.ToastUtils;
import com.runshu.newsmartark.util.widget.TabHostExt;


import java.util.Objects;

import cn.com.example.rfid.driver.Driver;
import cn.com.example.rfid.driver.RfidDriver;

import static com.runshu.newsmartark.common.ConstantUrl.Share_IP;

/**
 * Demo class
 *
 * @author yu
 * @date 5/13/21
 * @decs：
 */

public class SeetingManagerFragment extends BaseLazyFragmentPlus {
    TabHostExt tabhost;
    String[] spipow = { "5", "6", "7", "8", "9", "10", "11",
            "12", "13", "14", "15", "16", "17", "18", "19",
            "20", "21", "22", "23", "24", "25", "26", "27",
            "28", "29", "30", "31", "32", "33" };
    String[] strInvModel = {"模式1: 多标签", "模式2: 快速盘点", "模式3: 低功耗", "模式4: 测试", "模式5: 自适应"};
    String[] strTagInfo = {"EPC", "EPC + TID", "EPC + USR", "EPC + TID + USR", "EPC + TID + RFU", "EPC + RFU"};
    String[] strSession = {"S0", "S1", "S2", "S3"};
    String[] strTarget = {"A", "B"};
    Driver driver;
    BoxBaseActivity mReader;
    private ArrayAdapter<String> arradp_pow, arradp_reg, arradp_bank, arradp_lock,arradp_bank_setf,
            arradp_rw, arradp_itemlock, arradp_invmodel, arradp_taginfo, arradp_session, arradp_target;

    CheckBox CBAnt1, CBAnt2, CBAnt3, CBAnt4, CBAnt5, CBAnt6, CBAnt7, CBAnt8, CBAnt9, CBAnt10, CBAnt11, CBAnt12, CBAnt13, CBAnt14, CBAnt15, CBAnt16, CBTmp, CBFastID;
    Button bGetLoss, bGetAnt, bSetAnt, bGetTmp, bGetTmpP, bSetTmpP, bCheckAnt, bGetFastID, bSetFastID;
    EditText EtGetLoss, EtGetTmp;
    TextView TVAnt1, TVAnt2, TVAnt3, TVAnt4, TVAnt5, TVAnt6, TVAnt7, TVAnt8, TVAnt9, TVAnt10,
            TVAnt11, TVAnt12, TVAnt13, TVAnt14, TVAnt15, TVAnt16;


    //功率部分
    Button button_getantpower, button_setantpower,button_setCycleNum,button_getCycleNum;
    Spinner spinner_ant1rpow;
    EditText CycleANum,CycleBNum;



    //模式设置部分
    Button bInvModelSet, bInvModelGet, bTagInfoSet, bTagInfoGet, bSessionGet, bSessionSet;
    Spinner spInvModel, spTagInfo, spSession, spTarget;
    CheckBox cbInvModel, cbTagInfo;


    // GPIO
    Button bOpenDoor, bCloseDoor, bOpenLight, bCloseLight, bGetGPIOStatus;
    RadioButton rBDoorOpen, rBDoorClose, rBLightOpen, rBLightClose;

    //ip
    EditText Et_ip;
    Button Bt_ip;

    @Override
    protected int getLayoutId() {
        return R.layout.fra_seeting;
    }

    @Override
    protected void initData() {

        driver = new RfidDriver();
        mReader = BoxBaseActivity.ctx;
//        mReader.SetCloseDoorMode(0);				// 设置电磁锁检测方式



    }

    @Override
    protected void initView(View layoutInflateView) {
        tabhost =  Objects.requireNonNull(getView()).findViewById(R.id.tabhost4);
        tabhost.setup();
        tabhost.getTabWidget().setOrientation(LinearLayout.VERTICAL);

        tabhost.addTab(tabhost.newTabSpec("tab1")
                .setIndicator(createIndicatorView(context, tabhost, "天线功率"))
                .setContent(R.id.tab4_sub1_antpow));

        tabhost.addTab(tabhost.newTabSpec("tab2")
                .setIndicator(createIndicatorView(context, tabhost, "天线设置"))
                .setContent(R.id.tab4_sub2_gen2));

        tabhost.addTab(tabhost.newTabSpec("tab3")
                .setIndicator(createIndicatorView(context, tabhost, "模式设置"))
                .setContent(R.id.tab4_sub3_ModelSetting));
        tabhost.addTab(tabhost.newTabSpec("tab4")
                .setIndicator(createIndicatorView(context, tabhost, "GPIO"))
                .setContent(R.id.tab4_sub4_gpio));
        tabhost.addTab(tabhost.newTabSpec("tab5")
                .setIndicator(createIndicatorView(context, tabhost, "IP设置"))
                .setContent(R.id.tab4_sub5_ip));
        tabhost.setCurrentTab(0);
        InitAntLoss();
        InitModelControl();
        InitGPIO();
        InitPwr();
        InitIP();
    }

    private View createIndicatorView(Context context, TabHost tabHost,
                                     String title) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View tabIndicator = inflater.inflate(R.layout.tab_indicator_vertical,
                tabHost.getTabWidget(), false);
        final TextView titleView = (TextView) tabIndicator
                .findViewById(R.id.tv_indicator);
        titleView.setText(title);
        return tabIndicator;
    }
    private void InitPwr(){
        spinner_ant1rpow = (Spinner) getView().findViewById(R.id.spinner_ant1rpow);

        arradp_pow = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, spipow);
        arradp_pow
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_ant1rpow.setAdapter(arradp_pow);

        button_getantpower = (Button) getView().findViewById(R.id.button_antpowget);
        button_setantpower = (Button) getView().findViewById(R.id.button_antpowset);

        CycleANum = getView().findViewById(R.id.Et_CycleANum);
        CycleBNum = getView().findViewById(R.id.Et_CycleBNum);
        button_setCycleNum  = getView().findViewById(R.id.button_setCycleNum);
        button_getCycleNum = getView().findViewById(R.id.button_getCycleNum);

    }
    private void InitAntLoss(){
        CBAnt1 		= getView().findViewById(R.id.cB_Ant1);
        CBAnt2 		= getView().findViewById(R.id.cB_Ant2);
        CBAnt3 		= getView().findViewById(R.id.cB_Ant3);
        CBAnt4 		= getView().findViewById(R.id.cB_Ant4);
        CBAnt5 		= getView().findViewById(R.id.cB_Ant5);
        CBAnt6 		= getView().findViewById(R.id.cB_Ant6);
        CBAnt7 		= getView().findViewById(R.id.cB_Ant7);
        CBAnt8 		= getView().findViewById(R.id.cB_Ant8);
        CBAnt9 		= getView().findViewById(R.id.cB_Ant9);
        CBAnt10 	= getView().findViewById(R.id.cB_Ant10);
        CBAnt11 		= getView().findViewById(R.id.cB_Ant11);
        CBAnt12 		= getView().findViewById(R.id.cB_Ant12);
        CBAnt13 		= getView().findViewById(R.id.cB_Ant13);
        CBAnt14 		= getView().findViewById(R.id.cB_Ant14);
        CBAnt15 		= getView().findViewById(R.id.cB_Ant15);
        CBAnt16 		= getView().findViewById(R.id.cB_Ant16);

        bGetLoss 	= getView().findViewById(R.id.Bt_GetLoss);
        EtGetLoss 	= getView().findViewById(R.id.Et_Loss);

        bGetAnt 	= getView().findViewById(R.id.Bt_GetAnt);
        bSetAnt 	= getView().findViewById(R.id.Bt_SetAnt);

        bCheckAnt   = getView().findViewById(R.id.Bt_CheckAnt);

        TVAnt1		= getView().findViewById(R.id.Tv_Ant1);
        TVAnt2		= getView().findViewById(R.id.Tv_Ant2);
        TVAnt3 		= getView().findViewById(R.id.Tv_Ant3);
        TVAnt4 		= getView().findViewById(R.id.Tv_Ant4);
        TVAnt5		= getView().findViewById(R.id.Tv_Ant5);
        TVAnt6 		= getView().findViewById(R.id.Tv_Ant6);
        TVAnt7		= getView().findViewById(R.id.Tv_Ant7);
        TVAnt8		= getView().findViewById(R.id.Tv_Ant8);
        TVAnt9		= getView().findViewById(R.id.Tv_Ant9);
        TVAnt10		= getView().findViewById(R.id.Tv_Ant10);
        TVAnt11		= getView().findViewById(R.id.Tv_Ant11);
        TVAnt12		= getView().findViewById(R.id.Tv_Ant12);
        TVAnt13		= getView().findViewById(R.id.Tv_Ant13);
        TVAnt14		= getView().findViewById(R.id.Tv_Ant14);
        TVAnt15		= getView().findViewById(R.id.Tv_Ant15);
        TVAnt16		=getView(). findViewById(R.id.Tv_Ant16);
    }
    private void InitModelControl(){


        //盘点模式部分
        spInvModel = getView().findViewById(R.id.sp_InvModel);

        arradp_invmodel = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, strInvModel);
        arradp_invmodel
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spInvModel.setAdapter(arradp_invmodel);

        bInvModelGet = getView().findViewById(R.id.bt_InvModelGet);
        bInvModelSet = getView().findViewById(R.id.bt_InvModelSet);

        cbInvModel = getView().findViewById(R.id.cb_InvModel);

        //标签数据信息部分
        spTagInfo = getView().findViewById(R.id.sp_TagInfo);

        arradp_taginfo = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, strTagInfo);
        arradp_taginfo
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spTagInfo.setAdapter(arradp_taginfo);

        bTagInfoGet = getView().findViewById(R.id.bt_TagInfGet);
        bTagInfoSet = getView().findViewById(R.id.bt_TagInfSet);

        cbTagInfo = getView().findViewById(R.id.cb_TagInfo);



        CBFastID		= getView().findViewById(R.id.cB_FastID);
        bGetFastID	= getView().findViewById(R.id.Bt_GetFastID);
        bSetFastID	= getView().findViewById(R.id.Bt_SetFastID);

        //Session部分
        spSession = getView().findViewById(R.id.sp_Session);

        arradp_session = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, strSession);
        arradp_session
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spSession.setAdapter(arradp_session);

        spTarget = getView().findViewById(R.id.sp_Target);

        arradp_target = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, strTarget);
        arradp_target
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spTarget.setAdapter(arradp_target);

        bSessionGet = getView().findViewById(R.id.Bt_GetSession);
        bSessionSet = getView().findViewById(R.id.Bt_SetSession);
    }

    private void InitGPIO(){
        bOpenDoor 	= getView().findViewById(R.id.Bt_OpenDoor);
        bCloseDoor 	= getView().findViewById(R.id.Bt_CloseDoor);
        bOpenLight 	= getView().findViewById(R.id.Bt_OpenLight);
        bCloseLight = getView().findViewById(R.id.Bt_CloseLight);
        bGetGPIOStatus 	=getView().findViewById(R.id.Bt_GetGPIOStatue);
        rBDoorOpen 		= getView().findViewById(R.id.radioButton_DoorOpen);
        rBDoorClose		= getView().findViewById(R.id.radioButton_DoorClose);
    }
    private void InitIP(){
        Et_ip = getView().findViewById(R.id.Et_ip);
        Bt_ip = getView().findViewById(R.id.Bt_ip);

        Et_ip.setText((String)SharedPreferencesUtils.get(context, Share_IP, "http://pre.cloud.iot.runshutech.com"));
    }

    @Override
    protected void bindListener() {



        //设置A循环次数
        button_setCycleNum.setOnClickListener(view ->

        {
            //设置A循环次数
            int CycleANumInt = Integer.parseInt(CycleANum.getText().toString().trim().equals("")?"0":CycleANum.getText().toString().trim());
            if (CycleANumInt==0){
                Toast.makeText(context, "循环次数最少1次", Toast.LENGTH_SHORT).show();
                return;
            }
            SharedPreferencesUtils.put( MyApplication.getApplication(), ConstantUrl.Share_CycleANum, CycleANumInt );
            //设置B循环次数
            int CycleBNumInt = Integer.parseInt(CycleBNum.getText().toString().trim().equals("")?"0":CycleBNum.getText().toString().trim());
            if (CycleBNumInt==0){
                Toast.makeText(context, "循环次数最少1次", Toast.LENGTH_SHORT).show();
                return;
            }
            SharedPreferencesUtils.put( MyApplication.getApplication(), ConstantUrl.Share_CycleBNum, CycleBNumInt );
            Toast.makeText(context, "设置循环次数成功", Toast.LENGTH_SHORT).show();
        });

        //获取循环次数
        button_getCycleNum.setOnClickListener(view -> {
            CycleANum.setText(String.valueOf((int) SharedPreferencesUtils.get( MyApplication.getApplication(), ConstantUrl.Share_CycleANum, 6)));
            CycleBNum.setText(String.valueOf((int) SharedPreferencesUtils.get( MyApplication.getApplication(), ConstantUrl.Share_CycleBNum, 6 )));
        });

        //获取功率
        button_getantpower.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                String text = driver.GetTxPower();

                if(text.equals("-1020")){
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(text.equals("-1000")){
                    Toast.makeText(context, "设备未连接", Toast.LENGTH_SHORT).show();
                    return;
                }

                String[] PowArrary = text.split(",");
                String text1 = PowArrary[0].substring(6);
                int i = Integer.parseInt(text1,10) - 5;

                spinner_ant1rpow.setSelection(i);

                {
                    Toast.makeText(context, "获取成功" + text, Toast.LENGTH_SHORT).show();
                }
            }

        });

        //设置功率
        button_setantpower.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                int power = spinner_ant1rpow.getSelectedItemPosition()*1 + 5;
                int val = power;

                // 读功率和写功率设置一样，设置所有天线的发射功率
                // 如需单独设置某个天线的发射功率，把天线号设置为相应的数字，如设置天线3的发射功率，ant = 3
                boolean result = mReader.SetReaderTxPower(val, val, 0);
                if(result == true){
                    Toast.makeText(context, "发射功率设置成功", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(context, "发射功率设置失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //天线反射
        bGetLoss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int AntLoss = driver.GetAntLoss();

                if(-1000 == AntLoss){
                    Toast.makeText(context, "设备未连接", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(-1001 == AntLoss){
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(-1020 == AntLoss){
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                    return;
                }

                EtGetLoss.setText(String.valueOf(AntLoss));
            }
        });

        //设置天线
        bSetAnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int[] ant = new int[100];
                int cur = 0;
                if (CBAnt1.isChecked()){
                    ant[cur++] = 1;
                }
                if (CBAnt2.isChecked()){
                    ant[cur++] = 2;
                }
                if (CBAnt3.isChecked()){
                    ant[cur++] = 3;
                }
                if (CBAnt4.isChecked()){
                    ant[cur++] = 4;
                }
                if (CBAnt5.isChecked()){
                    ant[cur++] = 5;
                }
                if (CBAnt6.isChecked()){
                    ant[cur++] = 6;
                }
                if (CBAnt7.isChecked()){
                    ant[cur++] = 7;
                }
                if (CBAnt8.isChecked()){
                    ant[cur++] = 8;
                }
                if (CBAnt9.isChecked()){
                    ant[cur++] = 9;
                }
                if (CBAnt10.isChecked()){
                    ant[cur++] = 10;
                }
                if (CBAnt11.isChecked()){
                    ant[cur++] = 11;
                }
                if (CBAnt12.isChecked()){
                    ant[cur++] = 12;
                }
                if (CBAnt13.isChecked()){
                    ant[cur++] = 13;
                }
                if (CBAnt14.isChecked()){
                    ant[cur++] = 14;
                }
                if (CBAnt15.isChecked()){
                    ant[cur++] = 15;
                }
                if (CBAnt16.isChecked()){
                    ant[cur++] = 16;
                }

                boolean result = mReader.SetReaderAntenna(ant, cur);

                if(false == result){
                    Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                else {
                    Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //获取天线
        bGetAnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String AntStatus;
                AntStatus = driver.Get_Ant();

                if(AntStatus.equals("-1000")) {
                    Toast.makeText(context, "设备未连接", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(AntStatus.equals("-1020")){
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                    return;
                }

                CBAnt1.setChecked(false);	CBAnt2.setChecked(false);	CBAnt3.setChecked(false);
                CBAnt4.setChecked(false);	CBAnt5.setChecked(false);	CBAnt6.setChecked(false);
                CBAnt7.setChecked(false);	CBAnt8.setChecked(false);	CBAnt9.setChecked(false);
                CBAnt10.setChecked(false);	CBAnt11.setChecked(false);	CBAnt12.setChecked(false);
                CBAnt13.setChecked(false);	CBAnt14.setChecked(false);	CBAnt15.setChecked(false);
                CBAnt16.setChecked(false);

                String[] AntArrary = AntStatus.split(",");
                for (int i = 0; i < AntArrary.length; i++){
                    switch (AntArrary[i]){
                        case "1":
                            CBAnt1.setChecked(true);
                            break;
                        case "2":
                            CBAnt2.setChecked(true);
                            break;
                        case "3":
                            CBAnt3.setChecked(true);
                            break;
                        case "4":
                            CBAnt4.setChecked(true);
                            break;
                        case "5":
                            CBAnt5.setChecked(true);
                            break;
                        case "6":
                            CBAnt6.setChecked(true);
                            break;
                        case "7":
                            CBAnt7.setChecked(true);
                            break;
                        case "8":
                            CBAnt8.setChecked(true);
                            break;
                        case "9":
                            CBAnt9.setChecked(true);
                            break;
                        case "10":
                            CBAnt10.setChecked(true);
                            break;
                        case "11":
                            CBAnt11.setChecked(true);
                            break;
                        case "12":
                            CBAnt12.setChecked(true);
                            break;
                        case "13":
                            CBAnt13.setChecked(true);
                            break;
                        case "14":
                            CBAnt14.setChecked(true);
                            break;
                        case "15":
                            CBAnt15.setChecked(true);
                            break;
                        case "16":
                            CBAnt16.setChecked(true);
                            break;
                        default:
                            break;
                    }
                }
                Toast.makeText(context, "获取成功" + AntStatus, Toast.LENGTH_SHORT).show();
            }
        });

        //天线检测
        bCheckAnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TVAnt1.setTextColor(Color.BLACK);
                TVAnt2.setTextColor(Color.BLACK);
                TVAnt3.setTextColor(Color.BLACK);
                TVAnt4.setTextColor(Color.BLACK);
                TVAnt5.setTextColor(Color.BLACK);
                TVAnt6.setTextColor(Color.BLACK);
                TVAnt7.setTextColor(Color.BLACK);
                TVAnt8.setTextColor(Color.BLACK);
                TVAnt9.setTextColor(Color.BLACK);
                TVAnt10.setTextColor(Color.BLACK);
                TVAnt11.setTextColor(Color.BLACK);
                TVAnt12.setTextColor(Color.BLACK);
                TVAnt13.setTextColor(Color.BLACK);
                TVAnt14.setTextColor(Color.BLACK);
                TVAnt15.setTextColor(Color.BLACK);
                TVAnt16.setTextColor(Color.BLACK);

                String[] AntArrary = mReader.CheckReaderPortStatus();

                for (int i = 0; i < AntArrary.length; i++){
                    switch (AntArrary[i]){
                        case "1":
                            TVAnt1.setTextColor(Color.GREEN);
                            break;
                        case "2":
                            TVAnt2.setTextColor(Color.GREEN);
                            break;
                        case "3":
                            TVAnt3.setTextColor(Color.GREEN);
                            break;
                        case "4":
                            TVAnt4.setTextColor(Color.GREEN);
                            break;
                        case "5":
                            TVAnt5.setTextColor(Color.GREEN);
                            break;
                        case "6":
                            TVAnt6.setTextColor(Color.GREEN);
                            break;
                        case "7":
                            TVAnt7.setTextColor(Color.GREEN);
                            break;
                        case "8":
                            TVAnt8.setTextColor(Color.GREEN);
                            break;
                        case "9":
                            TVAnt9.setTextColor(Color.GREEN);
                            break;
                        case "10":
                            TVAnt10.setTextColor(Color.GREEN);
                            break;
                        case "11":
                            TVAnt11.setTextColor(Color.GREEN);
                            break;
                        case "12":
                            TVAnt12.setTextColor(Color.GREEN);
                            break;
                        case "13":
                            TVAnt13.setTextColor(Color.GREEN);
                            break;
                        case "14":
                            TVAnt14.setTextColor(Color.GREEN);
                            break;
                        case "15":
                            TVAnt15.setTextColor(Color.GREEN);
                            break;
                        case "16":
                            TVAnt16.setTextColor(Color.GREEN);
                            break;
                        default:
                            break;
                    }
                }

                Toast.makeText(context, "获取成功", Toast.LENGTH_SHORT).show();
            }
        });

        //获取盘点模式
        bInvModelGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int val = driver.Inventory_Model_Get();

                if(-1000 == val){
                    Toast.makeText(context, "设备未连接", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(-1020 == val){
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                    return;
                }

//				if(0 == val){
//					Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
//					return;
//				}

                spInvModel.setSelection(val);
                Toast.makeText(context, "获取成功", Toast.LENGTH_SHORT).show();
            }
        });

        //设置盘点模式
        bInvModelSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int poisiton = spInvModel.getSelectedItemPosition();
                if(driver.Inventory_Model_Set(poisiton, cbInvModel.isChecked())){
                    Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
                }
            }
        });



        //获取Session
        bSessionGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String val = driver.GetGen2Para();

                if(val.equals("-1000")){
                    Toast.makeText(context, "设备未连接", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(val.equals("-1020")){
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                    return;
                }

                int tmp = Integer.parseInt(val.substring(6,7),16);
                tmp = (tmp & 0x03);
                spSession.setSelection(tmp);

                tmp = Integer.parseInt(val.substring(7,8),16);
                tmp = (tmp >> 3) & 0x01;
                spTarget.setSelection(tmp);

                Toast.makeText(context, "获取成功 " + val, Toast.LENGTH_SHORT).show();
            }
        });

        //设置Session和target
        bSessionSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int session = spSession.getSelectedItemPosition();
                int target = spTarget.getSelectedItemPosition();

                boolean result = mReader.SetReaderSession(session, target);
                if (result == true){
                    Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 开门
        bOpenDoor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean res = mReader.SetDoorStatus(0);

                if(res == true){
                    Toast.makeText(context, "开门成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context, "开门失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 关门
        bCloseDoor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                boolean res = mReader.SetDoorStatus(0);
//
//                if(res == true){
//                    Toast.makeText(context, "关门成功", Toast.LENGTH_SHORT).show();
//                }else{
//                    Toast.makeText(context, "关门失败", Toast.LENGTH_SHORT).show();
//                }
            }
        });

        // 开灯
        bOpenLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                boolean res = mReader.SetLightStatus(1);

//                if(res == true){
//                    Toast.makeText(context, "开灯成功", Toast.LENGTH_SHORT).show();
//                }else{
//                    Toast.makeText(context, "关灯失败", Toast.LENGTH_SHORT).show();
//                }
            }
        });

        // 关灯
        bCloseLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                boolean res = mReader.SetLightStatus(0);
//
//                if(res == true){
//                    Toast.makeText(context, "关灯成功", Toast.LENGTH_SHORT).show();
//                }else{
//                    Toast.makeText(context, "关灯失败", Toast.LENGTH_SHORT).show();
//                }
            }
        });

        // 获取门状态
        bGetGPIOStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int result = driver.GetGPIOStatus( 0x10);				// 手动获取门状态

                if ((result & 0x0010) != 0x0010){							// 门处于打开状态
                    rBDoorOpen.setChecked(true);
                    rBDoorClose.setChecked(false);
                }
                else {
                    rBDoorOpen.setChecked(false);
                    rBDoorClose.setChecked(true);
                }
            }
        });

        // ip配置
        Bt_ip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferencesUtils.put(context,Share_IP, Et_ip.getText().toString().trim());
                ToastUtils.showText("IP配置成功！");
            }
        });

    }

}